<?php
//$_SESSION['user_id'] = 8;
//$_SESSION['user_vipcard_number'] = "59001770";
//$_SESSION['user_moneycard_number'] = "152791152209428";
//$_SESSION['user_wxid'] = "o6PzEwuIC25N2V8pLJk8P_iIu1oc";
if(!isset($_SESSION['user_id'])){
    pe_goto('hotel.php?mod=login&act=wx_login&page=index');
}
//include ('pe['host_root']/hook/wxlogin.hook.php');

switch ($act) {
    //#####################@ 修改分类 @#####################//
    case 'pay_way':


        $order = $db->pe_select('order',array('order_id'=>$_g_order_id));
        //赋值
        $_p_info['order_fj_num'] = $order['order_fj_num'];
        $_p_info['final_money'] = $order['order_endprice'];
        //微信支付金额
        $_SESSION['money'] =  $order['order_endprice'];

        $moneycard = $db->pe_select('moneycard',array('moneycard_uid'=>$_SESSION['user_id']));

        //判断当前账户余额是否足够支付订单
        if($moneycard['moneycard_money']>$_p_info['final_money']){
            $money_enough = 1;
        }else{
            $money_enough = 0;
        }
        include(pe_tpl('pay_way.html'));
        break;

    case 'cost_success':
        $cost_money = $_SESSION['money'];
        $user_id = $_SESSION['user_id'];
        $order_id = $_SESSION['order_id'];
        $moneycard = $db->pe_select('moneycard',array('moneycard_uid'=>$user_id));
        $money = $moneycard['moneycard_money'] - $_SESSION['money'];
        $web = "hotel.php?mod=pay&act=pay_way&indate=".$_g_indate."&outdate=".$_g_outdate."&order_id=".$order_id;
        if($_SESSION['user_moneycard_number']!=""){

            if($moneycard['moneycard_money']>$cost_money){
                $db->pe_update('moneycard',array('moneycard_id'=>$moneycard['moneycard_id']),array('moneycard_money'=>$money));
                $time = date('Y-m-d H-i-s',time());

                if($db->pe_update('order',array('order_id'=>$order_id),array('order_ostate'=>1,'order_payway'=>2,'order_paytime'=>$time))){
                    ob_clean();
                    $db->pe_insert('moneycard_record',array('record_money'=>$_SESSION['money'],'record_card_num'=>$moneycard['moneycard_number'],'record_type'=>2,'record_time'=>time(),'record_uid'=>$_SESSION['user_id']));
                    pe_success('储蓄卡支付成功','hotel.php?mod=personal_center&act=indent_detail&order_id='.$order_id);
                }

            }else{
                ob_clean();
                pe_error('支付失败,储蓄卡余额不足',$web);
            }
        }else{
            pe_error('尚未开通储蓄卡，请千万个人中心开通',$web);
        }



        break;

    case 'add':

        if (isGet()) {


        } else if (isPost()) {


        }

        break;


    case'zouxujia':
//        $_SESSION['self_real_price'] = $_SESSION['money'];
        $_SESSION['self_real_price'] = 0.01;
        Header("Location: http://www.ktxwxgy.com/example/simplePay.php");
        break;

    case'pay_success':
        $order_id = $_SESSION['order_id'];
        $time = date('Y-m-d H-i-s',time());
        if($db->pe_update('order',array('order_id'=>$order_id),array('order_ostate'=>1,'order_payway'=>3,'order_paytime'=>$time,'order_refund_number'=>$_SESSION['out_trade_no']))){
            pe_success('支付成功','hotel.php?mod=personal_center&act=indent_detail&order_id='.$order_id);
        }
        break;
    //#####################@ 分类列表 @#####################//
    default :

        //首页酒店顶部图片

        if (isGet()) {
            //判断是否是二次提交\
            $user_id = $_SESSION['user_id'];
            $_SESSION['is_sumbit'] = 0;
            //默认无折扣
            $user_vip_account = 1;
            $house = $db->pe_select('house', array('house_id' => $_g_house_id));
            $sql = 'select * from wxgg_redpack as r where r.redpack_houseid = '.$_g_house_id . ' and r.redpack_state != 1 and r.redpack_userid = '.$user_id;
            $redpack = $db->sql_selectall($sql);
//            mp($redpack);
            //判断是否有会员卡
            if(!empty($_SESSION['user_vipcard_number'])){
                $user_vipcard_number = 1;
            }else{
                $user_vipcard_number = 0;
            }

            //是会员的话要给房价折扣
            if($user_vipcard_number==1){
                $user_vip = $db->pe_select('vipcard',array('vipcard_uid'=>$user_id));
                //会员折扣
                $user_vip_account = $user_vip['vipcard_sale'];
            }
            //存入session折扣
            $_SESSION['user_vip_account'] = $user_vip_account;

            foreach ($redpack as $k => $v){
                if($v['redpack_enddate']>time()){
                    $redpack[$k]['is_effective'] = 1;
                }else{
                    $redpack[$k]['is_effective'] = 0;
                }
                $redpack[$k]['redpack_enddate'] = date('Y-m-d',$v['redpack_enddate']);
            }
//            mp($house);exit;

            include(pe_tpl('pay.html'));
        } else if (isPost()) {
            $data['order_daynum'] = diffBetweenTwoDays($_g_indate, $_g_outdate);
            $data['order_number'] = getMillisecond() . rand(10, 100);
            $data['order_houseid'] = $_g_house_id;
            $data['order_state'] = 0;
            $data['order_startdate'] = strtotime($_g_indate);
            $data['order_enddate'] = strtotime($_g_outdate);
            $data['order_subdate'] = date('Y-m-d H-i-s', time());
            $data['order_ostate'] = 0;
            $data['order_pw'] = time() . rand(10, 100);
            $data['order_fj_num'] = $_p_info['rooms'];
            $data['order_wxid'] = $_SESSION['user_wxid']; //微信ID
            $data['order_userid'] = $_SESSION['user_id'];
            $data['order_endprice'] = $_p_info['final_money'];
            $data['order_allprice'] = $_p_info['all_price'];
            $data['order_redpack_id'] = $_p_info['redpack_id'];
            $data['order_redpack_money'] = $_p_info['redpack_money'];
            $data['order_phone'] = $_p_info['order_phone'];
            $data['order_fj_num'] = $_p_info['order_fj_num'];
            $data['order_have_bre'] = $_p_info['is_have_bre'];
            if (!empty($_p_in_person)) {
                $data['order_name'] = implode(",",$_p_in_person);
            }
            if(!empty($_SESSION['user_vipcard_number'])){
                $data['order_isvip'] = 1;
                $data['order_vnorcid'] = $_SESSION['user_vipcard_number'];
            }

            $_SESSION['money'] =  $_p_info['final_money'];

            $moneycard = $db->pe_select('moneycard',array('moneycard_uid'=>$_SESSION['user_id']));

            //判断当前账户余额是否足够支付订单
            if($moneycard['moneycard_money']>$_p_info['final_money']){
                $money_enough = 1;
            }else{
                $money_enough = 0;
            }





//            //提交订单前再判断一次是否还有房
//
//            //房型房间数
//            $fj_info = $db->pe_selectall('fj', array('fj_houseid' => $_g_house_id));
//            $fj_num = count($fj_info);
//
//            //房型房间订单数
//            $sql2 = 'select ho.house_id,ho.house_name,group_concat(ord.order_number) as ord_number,count(ho.house_id)  as nums from wxgg_house as ho join wxgg_order as ord on ho.house_id = ord.order_houseid  where ord.order_startdate >= ' . strtotime($_g_indate) . ' and ord.order_startdate <= ' . strtotime($_g_outdate) . ' group by ho.house_id ';
//
//
//            //识别是否可以预定
//            foreach ($fj_info as $k => $v) {
//
//                if ($v['nums'] < $ord_info[$k]['nums']) {
//                    $fj_info[$k]['can_order'] = 0;
//
//                } else {
//                    $fj_info[$k]['can_order'] = 1;
//                }
//            }
//            //判断结束

            //红包已使用要更新红包状态
            $db->pe_update('redpack',array('redpack_id'=>$_p_info['redpack_id']),array('redpack_state'=>1));
            //更新完毕

            //插入订单数据
//            mp($_p_info);exit;
            if($_SESSION['is_sumbit']==0){
                $db->pe_insert('order', $data);
                $_SESSION['order_id'] = mysql_insert_id();
                $_SESSION['is_sumbit'] = 1;
            }


            include(pe_tpl('pay_way.html'));
        }


        break;
}
